#include "Renderer.h"
#include "IRenderable.h"


Renderer::Renderer(void)
{
}


Renderer::~Renderer(void)
{
	ClearQueue();
}

Renderer* Renderer::getInstance()
{
	static Renderer thisInstance;

	return &thisInstance;
}

void Renderer::AddRenderable( const IRenderable *pGeometry,Priority ePriority )
{
	switch(ePriority)
	{
	case P_FIRST:
		m_First.push(pGeometry);
		break;
	case P_SECOND:
		m_Second.push(pGeometry);
		break;
	case P_THIRD:
		m_Third.push(pGeometry);
		break;
	case P_FOURTH:
		m_Fourth.push(pGeometry);
		break;
	}
}

void Renderer::Render()
{
	//////////////////////////////////////////////////////////////////////////
	// Render everything from first to last
	while(!m_First.empty())
	{
		const IRenderable *pObj = m_First.front();
		m_First.pop();

		pObj->Render();
	}
	while(!m_Second.empty())
	{
		const IRenderable *pObj = m_Second.front();
		m_Second.pop();

		pObj->Render();
	}
	while(!m_Third.empty())
	{
		const IRenderable *pObj = m_Third.front();
		m_Third.pop();

		pObj->Render();
	}
	while(!m_Fourth.empty())
	{
		const IRenderable *pObj = m_Fourth.front();
		m_Fourth.pop();

		pObj->Render();
	}
	//////////////////////////////////////////////////////////////////////////
}

void Renderer::ClearQueue()
{
	while(!m_First.empty())		m_First.pop();
	while(!m_Second.empty())	m_Second.pop();
	while(!m_Third.empty())		m_Third.pop();
	while(!m_Fourth.empty())	m_Fourth.pop();
}
